"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 数据库运行时，删除pid文件，hung主进程
Create At   : 2020/07/23
@zou_jialiang0505328126
Description :
    1.执行TPCC2.删除pid文件3.hung住主进程4.等待10s5.重新连接主进程6.查看TPCC业务是否继续执行
Expect      :
    未修改原逻辑
History     :
    2020/07/23  created by @wan005
    modified ：2020-12-10 by @peilinqian ；kill_pid方法会导致tpcc进程也被被kill，该用例只需要kill数据库进程，修改kill数据库进程方法；增加tpcc结果打印
"""

import unittest
from yat.test import Node
import time
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()


class HungPidDeletePidfile(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    GS_CTL_STOP_SUCCESS_MSG = Constant.GS_CTL_STOP_SUCCESS_MSG
    STANDBY_NORMAL = Constant.STANDBY_NORMAL
    PRIMARY_NORMAL = Constant.PRIMARY_NORMAL
    START_SUCCESS_MSG = Constant.START_SUCCESS_MSG
    TPCC_PATH = macro.TPCC_PATH
    DB_POSTMASTER_PID_NAME = macro.DB_POSTMASTER_PID_NAME
    TPCC_SUCCESS_MSG = Constant.TPCC_SUCCESS_MSG
    RESTART_SUCCESS_MSG = Constant.RESTART_SUCCESS_MSG
    sh = CommonSH()
    standby_1_sh = CommonSH('Standby1DbUser')
    standby_2_sh = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case126.py start------------')
        logger.info("-----------this is setup-----------")

    def test_pid_delete_pidfile(self):
        logger.info("-----------start TPCC-----------")
        tpcc_thread = ComThread(common.startTPCC, args=(self.dbPrimaryRootNode, self.TPCC_PATH,))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        logger.info("-----------delete postmaster.pid -----------")
        delete_cmd = '''
            rm -rf {postmaster}'''.format(postmaster=self.DB_INSTANCE_PATH + '/' + self.DB_POSTMASTER_PID_NAME)
        self.dbPrimaryUserNode.sh(delete_cmd).result()

        logger.info('--------------------Hung pid-------------------------')
        common.kill_pid_keyword('gaussdb', 19, self.dbPrimaryUserNode)
        time.sleep(1)

        logger.info('--------------------sleep 15s-------------------------')
        time.sleep(15)

        logger.info('--------------------reconnect pid-------------------------')
        common.kill_pid_keyword('gaussdb', 18, self.dbPrimaryUserNode)
        time.sleep(1)

        logger.info('--------------------check TPCC-------------------------')
        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        logger.info(tpcc_result)
        self.assertTrue(tpcc_result.find(self.TPCC_SUCCESS_MSG) > -1)

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        self.sh.check_whether_remain_dirty_page()
        self.standby_1_sh.check_data_consistency()
        self.standby_2_sh.check_data_consistency()
